import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex);

// 配置数据存储到哪里
const vuexLocal = new VuexPersistence({
    storage: window.localStorage,
    reducer: state => ({
        user: state.user
    })
})

//要设置的全局访问的state对象
const state = {
    keepAlive: [],
    WuserName: "", //实名
    WidcCrd: "", //身份证号码
    stateValue: 1, //工人:1  工头:-1
    letter: false,
    prompt: false,
    realname: true,
    realsSatus: 1, //实名状态 0审核-1通过-2未通过
    city: [], //意向工作的城市
    cityID: [], //意向工作的城市ID
    typeWorkerId: 1,
    typeTatus: "", //工种状态数据
    order: {},
    temporary: {}, //临时数据集
    islogin: false,
    androidback: 1, //安卓返回键全局通用 1:正常,0：执行退出APP第一次地址，-1：执行退出APP
    menu: {
        active: 0 //菜单坐标
    },
    user: {
        username: '游客900_',
    },
    mapCache: false //地图缓存
};

const getters = {
    get_state_order() {
        return state.order
    },
    get_keepAlive() {
        return state.keepAlive
    },
    get_islogin() {
        return state.islogin
    },
    get_menu_active() {
        return state.menu.active
    },
    get_user() {
        state.user = JSON.parse(window.localStorage.getItem('user'));
        console.log(state.user)
        return state.user
    },
    get_androidback() {
        return state.androidback
    },
    get_state_typeTatus() {
        return state.typeTatus
    },
    //工人--工头
    get_stateValue() {
        var s = window.localStorage.getItem('stateValue');
        state.stateValue = s && s != 'null' ? s : 1;
        return state.stateValue
    },
    //获取临时数据集
    get_temporary() {
        return state.temporary
    },
    //实名认证状态
    get_state_realsSatus() {
        return state.realsSatus
    }
};
const mutations = {
    //获取临时数据集
    set_temporary(state, r) {
        state.temporary = r
    },
    set_islogin(state, r) {
        state.islogin = r;
    },
    set_keepAlive(state, r) {
        state.keepAlive = r;
    },
    set_menu_active(state, i) {
        state.menu.active = i;
    },
    set_user(state, user) {
        window.localStorage.setItem('user', JSON.stringify(user));
        state.user = user;
    },
    set_androidback(state, androidback) {
        state.androidback = androidback;
    },
    //设置工作地
    set_city(state, city) {
        state.city = city;
    },
    //设置工作地id
    set_cityID(state, cityID) {
        state.cityID = cityID;
    },
    // //设置工种状态
    set_typeTatus(state, typeTatus) {
        state.typeTatus = typeTatus;
    },
    //工人||工头
    set_stateValue(state, data) {
        window.localStorage.setItem('stateValue', data);
        state.stateValue = data
    },
    //实名状态
    set_realsSatus(state, realsSatus) {
        state.realsSatus = realsSatus
    }
};
const actions = {
    //获取临时数据集
    set_temporary(context, r) {
        context.commit('set_temporary', r);
    },
    set_islogin(context, r) {
        context.commit('set_islogin', r);
    },
    set_keepAlive(context, r) {
        context.commit('set_keepAlive', r);
    },
    set_menu_active(context, i) {
        context.commit('set_menu_active', i);
    },
    set_user(context, user) {
        context.commit('set_user', user);
    },
    set_androidback(context, androidback) {
        context.commit('set_androidback', androidback);
    },
    //实名状态
    set_realsSatus(context, realsSatus) {
        context.commit('set_realsSatus', realsSatus)
    },
    //工人||工头
    set_stateValue(context, data) {
        window.localStorage.setItem('stateValue', JSON.stringify(data));
        context.commit('set_stateValue', data)
            // state.stateValue = data
    },
};
const store = new Vuex.Store({
    state,
    getters,
    mutations,
    actions,
    //plugins: [vuexLocal.plugin]
});
export default store;